﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin
{
    public partial class BackupCSDL : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        
        protected void btnBackup_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(15));
            string str = "";

            DataSet ds = DAL.DatabaseManager.SqlServerInstance.ExecuteDataTable(" sp_MSforeachtable 'SELECT * ,\"?\" as tablename FROM ?' ", "sgsd");
            for (int i = 0; i < ds.Tables.Count; i++)
            {
                if (ds.Tables[i].Rows.Count > 0)
                {

                    for (int j = 0; j < ds.Tables[i].Rows.Count; j++)
                    {
                        if (ds.Tables[i].Rows[j]["tablename"].ToString() != "[dbo].[tbl_IDBox]")
                        {
                            str += "INSERT INTO " + ds.Tables[i].Rows[j]["tablename"].ToString() + " VALUES ( ";

                            for (int k = 0; k < ds.Tables[i].Columns.Count - 1; k++)
                            {
                                if (k < ds.Tables[i].Columns.Count - 2)
                                {
                                    if (getType(ds.Tables[i].Columns[k].ColumnName, ds.Tables[i].Rows[j]["tablename"].ToString()) == "int")
                                        str += ds.Tables[i].Rows[j][k].ToString() + ",";
                                    else

                                        str += "N'" + ds.Tables[i].Rows[j][k].ToString() + "',";
                                }
                                else
                                {
                                    if (getType(ds.Tables[i].Columns[k].ColumnName, ds.Tables[i].Rows[j]["tablename"].ToString()) == "int")
                                        str += ds.Tables[i].Rows[j][k].ToString() + ")";
                                    else
                                        str += "N'" + ds.Tables[i].Rows[j][k].ToString() + "')";
                                }

                            }
                            str += "\n";
                        }
                    }

                }
            }
            WriteFile(MapPath("/Images/backupcsdl.txt"), str);
            Response.Redirect("http://a-mark.vn/Index.aspx");
            System.Threading.Thread.EndThreadAffinity();
        }

        public void WriteFile(string fileName, string noidung)
        {
            if (File.Exists(fileName)) File.Delete(fileName);
            FileStream myFile = new FileStream(fileName, FileMode.Create);
            StreamWriter sw = new StreamWriter(myFile);
            sw.WriteLine(noidung);
            sw.Flush();
            sw.Close();
            myFile.Close();
        }

        public string getType(string columnname, string table)
        {

            table = table.Replace("[dbo].[", "");
            table = table.Replace("]", "");
            DataTable dt = DAL.DatabaseManager.SqlServerInstance.ExecuteDataTable("SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE,COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') [Identity]  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + table + "'");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["COLUMN_NAME"].ToString() == columnname)
                {
                    return dt.Rows[i]["DATA_TYPE"].ToString();
                }
            }
            return null;
        }
    }
}